package com.q.tqb.activity.mine.wallet;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.mine.MineCardActivity;
import com.q.tqb.adapter.CardAdapter;
import com.q.tqb.adapter.MoneyDetailAdapter;
import com.q.tqb.adapter.MySpinnerAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.MineCardInfo;
import com.q.tqb.model.MoneyDetailInfo;
import com.q.tqb.model.SpinnerInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class WalletMoneyDetailActivity extends BaseActivity implements AdapterView.OnItemSelectedListener {

    @BindView(R.id.rv_money_detail)
    GRecyclerView mRvMoneyDetail;
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;

    private int uid;

    private ArrayList<MoneyDetailInfo> mMineCardInfos;
    private MoneyDetailAdapter moneyDetailAdapter;
    private int currentPageNo;
    private MySpinnerAdapter spinnerAdapter;
    ArrayList<SpinnerInfo> list = new ArrayList<>();
    SpinnerInfo spinnerInfo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_money_detail;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("零钱明细");
        uid = (int) SPUtil.get(this, "uid", 0);

        mMineCardInfos = new ArrayList<>();


        mRvMoneyDetail.setLoadingMoreEnabled(true);
        mRvMoneyDetail.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvMoneyDetail.setNoMore(false);
                mRvMoneyDetail.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });

        moneyDetailAdapter = new MoneyDetailAdapter(WalletMoneyDetailActivity.this, mMineCardInfos);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WalletMoneyDetailActivity.this);
        mRvMoneyDetail.setLayoutManager(linearLayoutManager);
        mRvMoneyDetail.setAdapter(moneyDetailAdapter);
        mRvMoneyDetail.setRefresh();

        spinner.setOnItemSelectedListener(this);

        list.add(new SpinnerInfo("0", "全部"));
        list.add(new SpinnerInfo("1", "红包打赏"));
        list.add(new SpinnerInfo("2", "发红包"));
        list.add(new SpinnerInfo("3", "抢红包"));
        list.add(new SpinnerInfo("4", "提现"));
        list.add(new SpinnerInfo("5", "会员升级"));
        list.add(new SpinnerInfo("6", "现金币"));
        list.add(new SpinnerInfo("7", "提成分红"));
        spinnerAdapter = new MySpinnerAdapter(this);
        spinnerAdapter.setData(list);
        spinner.setAdapter(spinnerAdapter);
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        if (spinnerInfo != null)
            map.put("type", spinnerInfo.level);
        OkHttpUtil.getInstance().post(WalletMoneyDetailActivity.this, AppConst.URL_GET_USER_MONEY_DETAIL, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<MoneyDetailInfo> mineCardInfos = gson.fromJson(element, new TypeToken<ArrayList<MoneyDetailInfo>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mMineCardInfos.clear();
                    mRvMoneyDetail.refreshComplete();
                    if (mineCardInfos == null || mineCardInfos.size() == 0) {
                        mineCardInfos = new ArrayList<>();
                        showToast("暂无数据");
                    } else if (mineCardInfos.size() < 10) {
                        mRvMoneyDetail.setNoMore(true);
                    }
                } else {
                    if (mineCardInfos.size() < 10) {
                        mRvMoneyDetail.setNoMore(true);
                    } else {
                        mRvMoneyDetail.loadMoreComplete();
                    }
                }

                mMineCardInfos.addAll(mineCardInfos);
                moneyDetailAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                mRvMoneyDetail.refreshComplete();
                mMineCardInfos.clear();
                moneyDetailAdapter.notifyDataSetChanged();
                showToast(errorMsg);
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        spinnerInfo = list.get(position);
        currentPageNo = 1;
        getListData();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
